Repository接口及其子接口介绍
Repository接口
接口定义:
1 | public interface Repository<T, ID extends Serializable> { |
Repository接口是一个空接口,这个接口主要作为一个标记接口来捕获类型的工作,并帮助你发现继承这个接口的接口。
CrudRepository接口
CrudRepository接口继承Repository接口,实现了CRUD相关方法:
1 | public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { |
PagingAndSortingRepository接口
PagingAndSortingRepository接口继承CrudRepository接口,实现了分页排序相关的方法:
1 | public interface PagingAndSortingRepository<T, ID extends Serializable> |
JpaRepository接口
JpaRepository接口继承PagingAndSortingRepository接口,实现了JPA规范的相关方法:
1 | public interface JpaRepository<T, ID extends Serializable> |
使用方法命名规则查询
Spring Data JPA支持以特定的方法命名规则来生成查询。
下面是对JPA支持的关键字的概述,以及包含该关键字翻译成的sql片段。
Keyword | Sample | JPQL snippet |
---|---|---|
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname ,findByFirstnameIs ,findByFirstnameEquals | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 |
Containing | findByFirstnameContaining | … where x.firstname like ?1 |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection<Age> ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection<Age> ages) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
In和NotIn可以将Collection的任何子类作为参数以及数组或可变参数。
例子:
1 | public interface UserRepository extends Repository<User, Long> { |
使用按照方法命名规则,有弊端:
- 方法名会比较长: 约定大于配置
- 对于一些复杂的查询,是很难实现
使用@Query注解
对查询方法进行注释的查询优先于使用@NamedQuery定义的查询或者在orm.xml中声明的命名查询。
1 | public interface UserRepository extends JpaRepository<User, Long> { |
@Query注释允许通过将nativeQuery设置为true来执行原生sql查询。
1 | public interface UserRepository extends JpaRepository<User, Long> { |
@Query、@Modifying、@Transactional综合使用
使用@Query执行更新操作需要配合@Modifying注解和@Transactional注解。
事务一般在service层控制。
1 | interface UserRepository extends Repository<User, Long> { |
参考链接
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference